//
//  ViewController4.m
//  someDemo
//
//  Created by Reinhard-X on 2016/12/2.
//  Copyright © 2016年 上官惠阳. All rights reserved.
//

#import "ViewController4.h"
#import "UIButton+timer.h"
#import "UITextField+Add.h"

@interface ViewController4 ()<UITextFieldDelegate>

@end

@implementation ViewController4
{
    NSString *_verCode;
    NSString *_phoneNum;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _pwdField.delegate = self;

    //给scrBtn设置圆角
    self.scrBtn.layer.cornerRadius = 5;
    self.scrBtn.layer.masksToBounds = YES;
    [self setupForDismissKeyboard];
}
//点击发送验证码按钮触发的事件
- (IBAction)sendCodeAction:(id)sender {
    if (!_phoneField.text.length) {
        [_phoneField addShakeAnimation];

        [HUDTool showInView:self.view withText:@"请输入手机号"];
        return;
    }
    if (![QZChectTool isPhoneNumber:_phoneField.text]) {
        [HUDTool showInView:self.view withText:@"请输入正确格式的手机号"];
        return;
    }
    __weak typeof(self) weakSelf = self;
    //先判断手机是否已注册
    NSDictionary *dic = @{@"mobile":_phoneField.text,
                          @"token":@"Jg6uH5ZAkksqaFco"};
    [MHNetworkManager postReqeustWithURL:URL(@"?m=appface&c=sms&a=send_lost_code") params:dic successBlock:^(NSDictionary *returnData) {
        NSLog(@"returnData = %@",returnData);
        if (ResultJudge) {
            //拿到验证码
            _verCode = returnData[@"data"][@"code"];
            //拿到发送验证码的手机号
            _phoneNum = _phoneField.text;

            //给sendCodeBtn添加倒计时
            [weakSelf.sendCodeBtn addTimer];
        }else{
            ShowResultMsg;
        }
    } failureBlock:^(NSError *error) {
        DTLog(@"error = %@",error);
        ShowFailMsg;
    } showHUD:YES];
}
//点击完成按钮触发的事件
- (IBAction)scrBtnAction:(id)sender {
    if (!_phoneField.text.length) {
        [_phoneField addShakeAnimation];

        ShowSomeMsg(@"请输入手机号码");
        return;
    }
    if (![_phoneField.text isEqualToString:_phoneNum]) {
        ShowSomeMsg(@"请输入获取验证码的手机号");
        return;
    }
    if (!_verCodeField.text.length) {
        [_verCodeField addShakeAnimation];

        ShowSomeMsg(@"请输入验证码");
        return;
    }
     if (![_verCodeField.text isEqualToString:_verCode]) {
       ShowSomeMsg(@"验证码不正确");
        return;
    }
    if (!_pwdField.text.length) {
        [_pwdField addShakeAnimation];

        ShowSomeMsg(@"请输入密码");
        return;
    }
    __weak typeof(self) weakSelf = self;
    NSDictionary *dic = @{@"mobile":_phoneField.text,
                          @"pwd":_pwdField.text};
    [MHNetworkManager postReqeustWithURL:URL(@"?m=appface&c=user&a=resetpwd") params:dic successBlock:^(NSDictionary *returnData) {
        NSLog(@"returnData = %@",returnData);
        if (ResultJudge) {
            ShowSomeMsg(@"找回密码成功");

            //成功后退出
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            ShowResultMsg;
        }
    } failureBlock:^(NSError *error) {
        DTLog(@"error = %@",error);
        ShowFailMsg;
    } showHUD:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



- (IBAction)exitBtnAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
//编辑完,收回键盘,前提是interface ViewController4 () <UITextFieldDelegate>
{
    [textField resignFirstResponder];
    return YES;
}

@end
